Ext.QuickTips.init();
Ext.BLANK_IMAGE_URL = 'scripts/widgets/ext-2.2.1/resources/images/default/s.gif';

Ext.onReady(function(){
	var pnlHeader = new Ext.Panel({
		region : 'north',
		border : false,
		height : 73,
		items : {
			xtype : 'box',
			el : 'header'
		}
	});

	var pnlFooter = new Ext.Panel({
		region : 'south',
		border : false,
		height : 50,
		items : {
			xtype : 'box',
			el : 'footer'
		}
	});

	var pnlNavigate = new Ext.Panel({
        region: 'west',
        title: i18n.nav.navigationBar,
        margins: '5 0 5 5',
        split: true,
        collapsible: true,
        width: 200,
        layout:'accordion'
    });

	var pnlMain = new Ext.TabPanel( {
		region : 'center',
		margins:'5 5 5 0',
		enableTabScroll:true,
		resizeTabs:true,
        minTabWidth: 100,
        activeTab : 0,
        listeners : {
            beforeremove : function(tp, item) {  //当侦听到beforeremove事件时，把要关闭的Panel隐藏
            	tp.hideTabStripItem(item.hide());
            	this.stack.remove(item);
            	if (item == this.activeTab) {
		            var next = this.stack.next();
		            if (next) {
		                this.setActiveTab(next);
		            } else {
		                this.setActiveTab(0);
		            }
		        }
		        this.delegateUpdates();
            	return false;
            }
        },
        autoDestroy : false, //设置为 false 被关闭的Panel不会被自动释放
		items : [{id: 'tab-welcome', layout : 'fit', title : 'Welcome', closable : true}]
	});

	var queryInfo = {};
	queryInfo.queryObjectName = 'com.wef.system.entity.Module';
	queryInfo.fields = [];
	queryInfo.listType = 'list';

	var field = {};
	field.fieldName = 'moduleCode';
	field.fieldValueString = 'system';

	queryInfo.fields.push(field);
	
	var initProps = [];
	initProps.push('menuGroups');
	initProps.push('menuGroups.menuItems');
	initProps.push('moduleTitles');
	initProps.push('menuGroups.menuGroupTitles');
	initProps.push('menuGroups.menuItems.menuItemTitles');
	
	CommonService.findByQueryInit(queryInfo, initProps, function(modules){
		// load module
	    Ext.each(modules, function(module){
	    	var root = new Ext.tree.TreeNode({
				allowDrag:false,
				allowDrop:false
			});

	    	var pnlModule = new Ext.tree.TreePanel({
				title: getI18nTitle(module, 'Module'),
				border:false,
				width: 200,
				minSize: 175,
				maxSize: 400,
				rootVisible: false,
				root:root
			});

			//遍历菜单分组
			Ext.each(module.menuGroups, function(menuGroup){
				var menuGroupNode = new Ext.tree.TreeNode({
					id : menuGroup.menuGroupId,
		    		text: getI18nTitle(menuGroup, 'MenuGroup'),
		    		expanded: true,
		    		allowDrag:false
	    		});

		    	//遍历菜单项
		    	Ext.each(menuGroup.menuItems, function(menuItem){
		    		var menuItemNode = new Ext.tree.TreeNode({
		    			id : menuItem.menuItemId,
			    		text: getI18nTitle(menuItem, 'MenuItem'),
			    		url : menuItem.menuItemUrl,
			    		validateRequired : false,
			    		allowDrag:false
		    		});

		    		menuGroupNode.appendChild(menuItemNode);
		    	});

		    	root.appendChild(menuGroupNode);
			});

			Gfa.core.Initialize.add(pnlModule);
			Gfa.core.Initialize.init();
			pnlNavigate.add(pnlModule);
	    });

	    pnlNavigate.doLayout();
	});

	var viewport = new Ext.Viewport({
		layout : 'border',
		border : false,
		items : [pnlHeader, pnlFooter, pnlNavigate, pnlMain]
	});

	Gfa.core.Initialize.setMain(pnlMain);
	Gfa.core.Initialize.init();
});

function getI18nTitle(object, type){
	var titles = [];
	if(type=='Module'){
		titles = object.moduleTitles;
	}
	if(type=='MenuGroup'){
		titles = object.menuGroupTitles;
	}
	if(type=='MenuItem'){
		titles = object.menuItemTitles;
	}
	var defaultTitle = '';
	for(var index = 0, len = titles.length; index < len; index++){
		var title = titles[index];
		if(App.lang == title.lang){
			return title.title;
		}
		if('default' == title.lang){
			defaultTitle = title.title;
		}
	}
	return defaultTitle;
}
